!function(a) {
    var b = a.quiui;
    a.fn.dragDrop = function() {
        return b.run.call(this, "quiDrag", arguments, {
            idAttrName: "quiuidragid",
            hasElement: !1,
            propertyToElemnt: "target"
        });
    }, a.fn.quiGetDragManager = function() {
        return b.run.call(this, "quiGetDragManager", arguments, {
            idAttrName: "quiuidragid",
            hasElement: !1,
            propertyToElemnt: "target"
        });
    }, a.quiDefaults.Drag = {
        onStartDrag: !1,
        onDrag: !1,
        onStopDrag: !1,
        handler: null,
        proxy: !0,
        revert: !1,
        animate: !0,
        onRevert: null,
        onEndRevert: null,
        receive: null,
        onDragEnter: null,
        onDragOver: null,
        onDragLeave: null,
        onDrop: null,
        disabled: !1,
        proxyX: null,
        proxyY: null
    }, b.controls.Drag = function(a) {
        b.controls.Drag.base.constructor.call(this, null, a);
    }, b.controls.Drag.quiExtend(b.core.UIComponent, {
        __getType: function() {
            return "Drag";
        },
        __idPrev: function() {
            return "Drag";
        },
        _render: function() {
            var a = this, b = this.options;
            this.set(b), a.cursor = "move", a.handler.css("cursor", a.cursor), a.handler.bind("mousedown.drag", function(c) {
                b.disabled || 2 != c.button && a._start.call(a, c);
            }).bind("mousemove.drag", function() {
                b.disabled || a.handler.css("cursor", a.cursor);
            });
        },
        _rendered: function() {
            this.options.target.quiuidragid = this.id;
        },
        _start: function(c) {
            var d = this, e = this.options;
            if (!d.reverting && !e.disabled) {
                if (d.current = {
                    target: d.target,
                    left: d.target.offset().left,
                    top: d.target.offset().top,
                    startX: c.pageX || c.screenX,
                    startY: c.pageY || c.clientY
                }, 0 == d.trigger("startDrag", [ d.current, c ])) return !1;
                if (d._createProxy(e.proxy, c), e.proxy && !d.proxy) return !1;
                (d.proxy || d.handler).css("cursor", d.cursor), a(document).bind("selectstart.drag", function() {
                    return !1;
                }), a(document).bind("mousemove.drag", function() {
                    d._drag.apply(d, arguments);
                }), b.draggable.dragging = !0, a(document).bind("mouseup.drag", function() {
                    b.draggable.dragging = !1, d._stop.apply(d, arguments);
                });
            }
        },
        _drag: function(b) {
            var e, f, c = this;
            this.options, c.current && (e = b.pageX || b.screenX, f = b.pageY || b.screenY, 
            c.current.diffX = e - c.current.startX, c.current.diffY = f - c.current.startY, 
            (c.proxy || c.handler).css("cursor", c.cursor), c.receive && c.receive.each(function(d, g) {
                var h = a(g), i = h.offset();
                e > i.left && e < i.left + h.width() && f > i.top && f < i.top + h.height() ? c.receiveEntered[d] ? c.trigger("dragOver", [ g, c.proxy || c.target, b ]) : (c.receiveEntered[d] = !0, 
                c.trigger("dragEnter", [ g, c.proxy || c.target, b ])) : c.receiveEntered[d] && (c.receiveEntered[d] = !1, 
                c.trigger("dragLeave", [ g, c.proxy || c.target, b ]));
            }), c.hasBind("drag") ? 0 != c.trigger("drag", [ c.current, b ]) ? c._applyDrag() : c.proxy ? c._removeProxy() : c._stop() : c._applyDrag());
        },
        _stop: function(b) {
            var c = this, d = this.options;
            a(document).unbind("mousemove.drag"), a(document).unbind("mouseup.drag"), a(document).unbind("selectstart.drag"), 
            c.receive && c.receive.each(function(a, d) {
                c.receiveEntered[a] && c.trigger("drop", [ d, c.proxy || c.target, b ]);
            }), c.proxy && (d.revert ? c.hasBind("revert") ? 0 != c.trigger("revert", [ c.current, b ]) ? c._revert(b) : c._removeProxy() : c._revert(b) : (c._applyDrag(c.target), 
            c._removeProxy())), c.cursor = "move", c.trigger("stopDrag", [ c.current, b ]), 
            c.current = null, c.handler.css("cursor", c.cursor);
        },
        _revert: function(a) {
            var b = this;
            b.reverting = !0, b.proxy.animate({
                left: b.current.left,
                top: b.current.top
            }, function() {
                b.reverting = !1, b._removeProxy(), b.trigger("endRevert", [ b.current, a ]), b.current = null;
            });
        },
        _applyDrag: function(a) {
            var d, e, f, b = this, c = this.options;
            a = a || b.proxy || b.target, d = {}, e = !1, f = a == b.target, b.current.diffX && (d.left = f || null == c.proxyX ? b.current.left + b.current.diffX : b.current.startX + c.proxyX + b.current.diffX, 
            e = !0), b.current.diffY && (d.top = f || null == c.proxyY ? b.current.top + b.current.diffY : b.current.startY + c.proxyY + b.current.diffY, 
            e = !0), a == b.target && b.proxy && c.animate ? (b.reverting = !0, a.animate(d, function() {
                b.reverting = !1;
            })) : a.css(d);
        },
        _setReceive: function(b) {
            this.receiveEntered = {}, b && (this.receive = "string" == typeof b ? a(b) : b);
        },
        _setHandler: function(b) {
            var c = this, d = this.options;
            c.handler = b ? "string" == typeof b ? a(b, d.target) : b : a(d.target);
        },
        _setTarget: function(b) {
            this.target = a(b);
        },
        _setCursor: function(a) {
            this.cursor = a, (this.proxy || this.handler).css("cursor", a);
        },
        _createProxy: function(b, c) {
            if (b) {
                var d = this, e = this.options;
                "function" == typeof b ? d.proxy = b.call(this.options.target, d, c) : "clone" == b ? (d.proxy = d.target.clone().css({
                    position: "absolute",
                    opacity: .5,
                    "z-index": 1e3
                }), d.proxy.appendTo("body")) : (d.proxy = a("<div class='l-draggable'></div>"), 
                d.proxy.width(d.target.width()).height(d.target.height()), d.proxy.attr("dragid", d.id).appendTo("body")), 
                d.proxy.css({
                    left: null == e.proxyX ? d.current.left : d.current.startX + e.proxyX,
                    top: null == e.proxyY ? d.current.top : d.current.startY + e.proxyY
                }).show();
            }
        },
        _removeProxy: function() {
            var a = this;
            a.proxy && (a.proxy.remove(), a.proxy = null);
        }
    });
}(jQuery);